<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\api\v1\user;


use app\Request;
use app\services\other\QrcodeServices;
use app\services\system\attachment\SystemAttachmentServices;
use app\services\system\config\SystemConfigServices;
use app\services\user\UserBillServices;
use crmeb\services\UploadService;
use crmeb\services\wechat\MiniProgram;
use app\model\user\User as userModel;
use app\model\user\UserBill as userBillModel;


/**
 * 账单类
 * Class UserBill
 * @package app\controller\api\user
 */
class UserBill
{
    protected $services = NUll;

    /**
     * UserBill constructor.
     * @param UserBillServices $services
     */
    public function __construct(UserBillServices $services)
    {
        $this->services = $services;
    }

    /**
     * 推广佣金明细
     * @param Request $request
     * @param $type 0 全部  1 消费  2 充值  3 返佣  4 提现
     * @return mixed
     */
    public function spread_commission(Request $request, $type)
    {
        $uid = (int)$request->uid();
        return app('json')->successful($this->services->getUserBillList($uid, $type));
    }

    /**
     * 获取小程序二维码
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRoutineCode(Request $request)
    {
        $user = $request->user();
		$uid = (int)$request->uid();
        /** @var SystemAttachmentServices $systemAttachment */
        $systemAttachment = app()->make(SystemAttachmentServices::class);
        //小程序
        $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
        $imageInfo = $systemAttachment->getInfo(['name' => $name]);
        //检测远程文件是否存在
        if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
            $imageInfo = null;
            $systemAttachment->delete(['name' => $name]);
        }
        $siteUrl = sys_config('site_url');
        /** @var QrcodeServices $qrCode */
        $qrCode = app()->make(QrcodeServices::class);
        $res1 = $qrCode->qrCodeExist($user['uid'], 'spread');

        //图片在,数据库没有数据,删除图片
        if ($imageInfo && !$res1) {
            try {
                $uploadRes = UploadService::init($imageInfo->image_type);
                if ($imageInfo['image_type'] == 1) {
                    if (strpos($imageInfo['att_dir'], '/') == 0) {
                        $imageInfo['att_dir'] = substr($imageInfo['att_dir'], 1);
                    }
                    if ($imageInfo['att_dir']) $uploadRes->delete(public_path() . $imageInfo['att_dir']);
                } else {
                    if ($imageInfo['name']) $uploadRes->delete($imageInfo['name']);
                }
            } catch (\Throwable $e) {
            }
        }

        if (!$imageInfo || !$res1) {
			/** @var QrcodeServices $qrCode */
			$qrCode = app()->make(QrcodeServices::class);
			$resForever = $qrCode->qrCodeForever($user['uid'], 'spread-' . $user['uid']);
			$id = (int)$resForever['id'];
			/** @var QrcodeServices $QrcodeService */
			$QrcodeService = app()->make(QrcodeServices::class);
			//生成小程序地址
			$urlCode = $QrcodeService->getRoutineQrcodePath($id, $uid, 99, $name);
        } else {
			$urlCode = $imageInfo['att_dir'];
			if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
		}
        return app('json')->success(['url' => $urlCode]);
    }

    /**
     * 获取海报详细信息
     * @return mixed
     */
    public function getSpreadInfo(Request $request)
    {
        /** @var SystemConfigServices $systemConfigServices */
        $systemConfigServices = app()->make(SystemConfigServices::class);
        $spreadBanner = $systemConfigServices->getSpreadBanner() ?? [];
        $bannerCount = count($spreadBanner);
        $routineSpreadBanner = [];
        if ($bannerCount) {
            foreach ($spreadBanner as $item) {
                $routineSpreadBanner[] = ['pic' => $item];
            }
        }
		$uid = (int)$request->uid();
        if (sys_config('share_qrcode', 0) && request()->isWechat()) {
            /** @var QrcodeServices $qrcodeService */
            $qrcodeService = app()->make(QrcodeServices::class);
            if (sys_config('spread_share_forever', 0)) {
                $qrcode = $qrcodeService->getForeverQrcode('spread-' . $uid, $uid)->url;
            } else {
                $qrcode = $qrcodeService->getTemporaryQrcode('spread-' . $uid, $uid)->url;
            }
        } else {
            $qrcode = '';
        }
        return app('json')->success([
            'spread' => $routineSpreadBanner,
            'qrcode' => $qrcode,
			'avatar' => $request->user('avatar'),
            'nickname' => $request->user('nickname'),
            'site_name' => sys_config('site_name')
        ]);
    }

    /**
     * 积分记录
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function integral_list(Request $request)
    {
        $uid = (int)$request->uid();
        $top_str = (int)$request->param('top_str');
        return app('json')->successful($this->services->getIntegralList($uid,$top_str));
    }
    
    
    
    
    
    
    /**
     * 分享记录列表
     * @param Request $request
     * @return \think\Response
     */
    public function integral_share_list(Request $request)
    {
        $uid = (int)$request->uid();
        $param = $request->param();

        $param["uid"] = $uid;
        $data = userBillModel::getShareUserList($param);

        return app('json')->successful($data);
    }


    /**
     * 积分明细 - top 顶部数据
     * @param Request $request
     * @return \think\Response
     */
    public function integral_list_top(Request $request)
    {
        $uid = (int)$request->uid();
        $data["sum"] = userModel::where("uid", $uid)->value('integral') ?? 0;  //我的积分
        $gain_sum = userBillModel::where("uid", $uid)->where("pm", 1)->sum('number') ?? 0.00;  //收入   ->where("type",'in' , ['gain','share','system_add','sign'])
        $expend_sum = userBillModel::where("uid", $uid)->where("pm", 0)->sum('number') ?? 0.00;  //支出      ->where("type",'in' , ['storeIntegral_use', 'publish_job'])
        $sale_sum = userBillModel::where("uid", $uid)->where("type",'in' , ['gain'])->sum('number') ?? 0.00;  //消费所得
        $share_sum = userBillModel::where("uid", $uid)->where("type",'in' , ['share_new'])->sum('number') ?? 0.00;  //分享所得
        $data['gain_sum'] = floor($gain_sum);
        $data['expend_sum'] = floor($expend_sum);
        $data['sale_sum'] = floor($sale_sum);
        $data['share_sum'] = floor($share_sum);

        return app('json')->successful($data);
    }

}
